/* 
 * Jquery 自动完成插件简单版
 */
define(function (require, exports, module)
{
  var $ = require('jq_ext');
  var jxbaseinfo = require('jxbaseinfo');
  var jx_progressbar = require('jx_progressbar');
  var layer;
  var $msgbox =
  {
    //  默认配置
    defaults: {
      // url
      url: '',
      // ajax数据
      data: '',
      // 标题
      title: jxbaseinfo.title,
      // 数据提交类型
      type: jxbaseinfo.type,
      // 要提交的form id
      formid: jxbaseinfo.formid,
      content: null,
      contentdiv: "<div style='margin:10px 20px'>[[jx_content]]</div>",
      area: "auto",
      skin: "",
      offset: "auto",
      okValue: jxbaseinfo.okValue,
      cancelValue: jxbaseinfo.cancelValue
    },
    messageCode: "",
    messageType: 2,
    reserveMessage: "",
    layerbox: layer,
    close: function ()
    {
      layer.close(layer.index);
    },
    closeIndex: function (index)
    {
      layer.close(index);
    },
    closeAll: function ()
    {
      layer.closeAll();
    },
    show: function (config)
    {
      config = $.extend({}, this.defaults, config);
      layer.open({type: 2,
        title: this.defaults.title + "--->" + (this.defaults.title == config.title ? "" : config.title),
        content: config.url,
        area: config.area,
        offset: config.offset,
        skin: config.skin
      });
    },
    /**
     * 警告
     * @param   {String, HTMLElement}   消息内容
     * @param   {Function}              (可选) 回调函数
     */
    load: function (config, loadcallback)
    {
      // 合并默认配置
      config = $.extend({}, this.defaults, config);

      layer.load(2);

      msgbox_ajax(config, function (data)
      {
        loadcallback(data);
      });
    },
    /**
     * 显示弹出层
     * @param   index load序号
     */
    loadLayer: function (index)
    {
      layer.load(index);
    },    
    alertMobile: function (config, callback)
    {
      config = $.extend({}, this.defaults, config);
      if (null == config.content)
      {
        layer.load(2);
        msgbox_ajax(config, function (data)
        {
          layer.alert(data, {title: config.title,skin:config.skin}, callback);
        });
      } 
      else
      {
        layer.alert(config.content, {title: config.title,skin:config.skin}, function (index)
        {
          if (config.url != "")
          {
            msgbox_ajax(config, function (data)
            {
              layer.alert(data, {title: config.title,skin:config.skin}, callback);
            });
          } 
          else
          {
            if (null != callback)
              callback() ;
            $msgbox.close();
          }
        });
      }
    },
    /**
     * 警告
     * @param   {String, HTMLElement}   消息内容
     * @param   {Function}              (可选) 回调函数
     */
    alert: function (config, callback, callbackShow)
    {
      // 合并默认配置
      config = $.extend({}, this.defaults, config);
      if (null == config.content)
      {
        layer.load(2);
        msgbox_ajax(config, function (data)
        {
          layer.open({type: 1,
            title: config.title,
            content: data,
            area: config.area,
            offset: config.offset,
            skin: config.skin,
            btn: [config.okValue],
            yes: callback,
            success: callbackShow
          });
        });
      } 
      else
      {
        layer.alert(config.content, {title: config.title,skin:config.skin},
                function (index)
                {
                  if (config.url != "")
                  {
                    msgbox_ajax(config, function (data)
                    {
                      layer.open({type: 1,
                        title: config.title,
                        area: config.area,
                        offset: config.offset,
                        skin: config.skin,
                        content: data,
                        btn: [config.okValue],
                        yes: callback,
                        success: callbackShow
                      });
                    });
                  } 
                  else
								 {
										if (null != callback)
											callback() ;
                    $msgbox.close();
								 }
                }
        );
      }

    },
    /**
     * 警告
     * @param   {String, HTMLElement}   消息内容
     * @param   {Function}              (可选) 回调函数
     */
    login: function (config, okCallback, failCallback)
    {
      // 合并默认配置
      config = $.extend({}, this.defaults, config);
      layer.load(2);

      msgbox_ajax(config, function (data)
      {
        if ($msgbox.messageType == 0)
        {
          okCallback();
          $msgbox.close();
        } else
        {
          layer.open({type: 1,
            title: config.title,
            content: data,
            skin:config.skin,
            btn: [config.okValue],
            yes: failCallback
          });
        }
      });
    },
    prompt: function (config, callback)
    {
      config = $.extend({}, this.defaults, config);
      layer.open({type: 1,
        title: config.title,
        content: [
          '<div style="margin-bottom:10px 20px;font-size:12px">',
          config.content,
          '<br/>',
          '<input type="text" id="msgbox_input" class="form-control" value="',
          config.data,
          '" />',
          '</div>'
        ].join(''),
        btn: [config.okValue],
        area: config.area,
        offset: config.offset,
        skin: config.skin,
        yes: function ()
        {
          var inputObject = $(window.document).find("#msgbox_input");
          if (inputObject.length == 0)
            inputObject = $(window.top.document).find("#msgbox_input");
          return callback && callback.call(this, inputObject.val());
        }
      });
    },
    /**
     * 警告
     * @param   {String, HTMLElement}   消息内容
     * @param   {Function}              (可选) 回调函数
     */
    confirm: function (config, ok, cancel)
    {
      // 合并默认配置
      config = $.extend({}, this.defaults, config);
      if (null == config.content)
      {
        layer.load(2);
        msgbox_ajax(config, function (data)
        {
          if ($msgbox.messageType != 3)
          {
            layer.open({type: 1,
              title: config.title,
              content: data,
              area: config.area,
              offset: config.offset,
              skin: config.skin,
              btn: [config.okValue],
              yes: ok
            });
          } else
          {
            layer.open({type: 1,
              title: config.title,
              area: config.area,
              offset: config.offset,
              skin: config.skin,
              content: data,
              btn: [config.okValue, config.cancelValue],
              yes: ok,
              btn2: cancel
            });
          }
        });
      } else
      {
        if (config.url != "")
        {
          layer.open({type: 1,
            title: config.title,
            content: getContent(config, config.content),
            btn: [config.okValue, config.cancelValue],
            area: config.area,
            offset: config.offset,
            skin: config.skin,
            yes: function ()
            {
              $msgbox.close();
              layer.load(2);

              msgbox_ajax(config, function (data)
              {
                if ($msgbox.messageType != 3)
                {
                  layer.open({type: 1,
                    title: config.title,
                    content: data,
                    area: config.area,
                    offset: config.offset,
                    skin: config.skin,
                    btn: [config.okValue],
                    yes: ok
                  });
                } else
                {
                  layer.open({type: 1,
                    title: config.title,
                    content: data,
                    area: config.area,
                    offset: config.offset,
                    skin: config.skin,
                    btn: [config.okValue, config.cancelValue],
                    yes: ok,
                    cancel: cancel
                  });
                }
              });
            },
            btn2: cancel
          });
        } else
        {
          layer.confirm(config.content, {title: config.title},
                  ok,
                  cancel
                  );
        }
      }
    }
  };

  function msgbox_ajax(config, callback)
  {
    if (null == config)
      config = $msgbox.defaults;

    if (config.type.toLocaleLowerCase() == "post")
      config.data = $('#' + config.formid).serialize();
    jx_progressbar.show({}) ;
    $.ajax({
      url: config.url,
      type: config.type,
      data: config.data,
      cache: false,
      success: function (data, textStatus)
      {
        jx_progressbar.hide() ;
        layer.close(layer.index);
        if (undefined != $(data).find("#messageCode"))
          if (null != $(data).find("#messageCode"))
            $msgbox.messageCode = $(data).find("#messageCode").text();

        if (undefined != $(data).find("#messageType"))
          if (null != $(data).find("#messageType"))
            $msgbox.messageType = parseInt($(data).find("#messageType").text());

        if (undefined != $(data).find("#reserveMessage"))
          if (null != $(data).find("#reserveMessage"))
            $msgbox.reserveMessage = $(data).find("#reserveMessage").text();

        callback(data);
      },
      complete: function (XMLHttpRequest, textStatus)
      {
      },
      error: function (XMLHttpRequest, textStatus, errorThrown)
      {
        jx_progressbar.hide() ;
        layer.close(layer.index);
        layer.msg(textStatus + errorThrown, {icon: 2, time: 0, btn: [config.okValue]});
      }
    });
  }

  function getContent(config, content)
  {
    var contentdiv = config.contentdiv;
    return contentdiv.replace("[[jx_content]]", content);
  }
  function init(_layer)
  {
    layer = _layer;
    $msgbox.layerbox = _layer;
  }
  exports.msgbox = $msgbox;
  exports.init = init;

});